import { createStore } from 'vuex'

const state = {
  menuData: [
    {id: 1, name: '首页', icon: 'el-icon-s-home', path: '/home', children: [
      {id: 1, name: '概况', icon: 'el-icon-menu', path: '/survey', fullPath: '/home/survey'}
    ]},

    {id: 2, name: '商品', icon: 'el-icon-s-goods', path: '/goods', children: [
      {id: 1, name: '商品列表', icon: 'el-icon-menu', path: '/list', fullPath: '/goods/list'},
      {id: 2, name: '商品分类', icon: 'el-icon-menu', path: '/category', fullPath: '/goods/category'},
      {id: 3, name: '商品品牌', icon: 'el-icon-menu', path: '/brand', fullPath: '/goods/brand'},
      {id: 3, name: '商品参数', icon: 'el-icon-menu', path: '/parameter', fullPath: '/goods/parameter'},
      {id: 4, name: '商品评价', icon: 'el-icon-menu', path: '/comment', fullPath: '/goods/comment'},
      {id: 5, name: '相册管理', icon: 'el-icon-menu', path: '/photo', fullPath: '/goods/photo'},
    ]},

    {id: 3, name: '订单', icon: 'el-icon-s-order', path: '/order', children: [
      {id: 1, name: '订单列表', icon: 'el-icon-menu', path: '/list', fullPath: '/order/list'},
      {id: 2, name: '退款维权', icon: 'el-icon-menu', path: '/refund', fullPath: '/order/refund'},
    ]},

    {id: 4, name: '店铺', icon: 'el-icon-s-shop', path: '/shop', children: [
      {id: 1, name: '店铺管理', icon: 'el-icon-menu', path: '/manage', fullPath: '/shop/manage', children: [
        {id: 1, name: '店铺列表', path: '/list', fullPath: '/shop/manage/list'},
        {id: 2, name: '入驻申请', path: '/settled', fullPath: '/shop/manage/settled'},
        {id: 3, name: '续签申请', path: '/renewal', fullPath: '/shop/manage/renewal'}
      ]},
      {id: 2, name: '入驻帮助', icon: 'el-icon-menu', path: '/settled', fullPath: '/shop/settled', children: [
        {id: 1, name: '入驻协议', path: '/agreement', fullPath: '/shop/settled/agreement'}
      ]},
      {id: 3, name: '开店套餐', icon: 'el-icon-menu', path: '/package', fullPath: '/shop/package'},
      {id: 4, name: '主营行业', icon: 'el-icon-menu', path: '/industry', fullPath: '/shop/industry'},
      {id: 5, name: '服务保障', icon: 'el-icon-menu', path: '/guarantee', fullPath: '/shop/guarantee'}
    ]},
    {id:5,name:'会员',icon:'el-icon-s-custom',path:'/member',children:[
      {id: 1, name: '会员管理', icon:'el-icon-menu',path:'/profile',fullPath:'/member/profile'},
      {id: 2, name: '会员列表', icon:'el-icon-s-unfold',path:'/list',fullPath:'/member/list'},
      {id: 3, name: '会员等级', icon:'el-icon-trophy-1',path:'/level',fullPath:'/member/level'},
      {id: 4, name: '会员标签', icon:'el-icon-document',path:'/label',fullPath:'/member/label'},
      {id: 5, name: '注册协议', icon:'el-icon-document-copy',path:'/agreement',fullPath:'/member/agreement'},
      {id: 6, name: '黑名单', icon:'el-icon-receiving',path:'/blacklist',fullPath:'/member/blacklist'},
    ]},
    {id: 6, name: '营销', icon: 'el-icon-s-marketing', path: '/marketing', children: [
      {id: 1, name: '店铺营销', icon: 'el-icon-menu', path: '/shop', fullPath: '/marketing/shop'},
      {id: 2, name: '平台营销', icon: 'el-icon-monitor', path: '/platform', fullPath: '/marketing/platform',children:[
        {id: 1, name: '平台优惠券', path: '/coupon', fullPath: '/marketing/platform/coupon'},
        {id: 2, name: '限时秒杀', path: '/seckill', fullPath: '/marketing/platform/seckill'},
        {id: 3, name: '专题活动', path: '/activity', fullPath: '/marketing/platform/activity'},
      ]},
      {id: 3, name: '会员营销', icon: 'el-icon-menu', path: '/member', fullPath: '/marketing/member'},
      {id: 4, name: '应用工具', icon: 'el-icon-menu', path: '/utility', fullPath: '/marketing/utility'},
    ]},
    {id:7, name:'财务', icon:'el-icon-s-finance', path:'/finance',children:[
      {id:1, name:'财务概况',icon:'el-icon-document',path:'/overview',fullPath:'/finance/overview'},
      {id:2, name:'店铺结算',icon:'el-icon-bank-card',path:'/storesettlement',fullPath:'/finance/storesettlement'},
      {id:3, name:'店铺提现',icon:'el-icon-wallet',path:'/storeswithdrawal',fullPath:'/finance/storeswithdrawal'},
      {id:4, name:'会员提现',icon:'el-icon-takeaway-box',path:'/memberswithdrawal',fullPath:'/finance/memberswithdrawal'},
      {id:5, name:'入驻费用',icon:'el-icon-tickets',path:'/entryfee',fullPath:'/finance/entryfee'},
      {id:6, name:'分站转账',icon:'el-icon-money',path:'/substationtransfer',fullPath:'/finance/substationtransfer'},
      {id:7, name:'分站结算',icon:'el-icon-coin',path:'/substationsettlement',fullPath:'/finance/substationsettlement'},
      {id:8, name:'供应商结算',icon:'el-icon-set-up',path:'/suppliersettlement',fullPath:'/finance/suppliersettlement'},
      {id:9, name:'供应商提现',icon:'el-icon-postcard',path:'/supplierwithdrawal',fullPath:'/finance/supplierwithdrawal'},
      {id:10, name:'供应商入驻',icon:'el-icon-office-building',path:'/supplierentry',fullPath:'/finance/supplierentry'}
    ]},
    {id:9,name:'设置',icon:'el-icon-s-tools',path:'/set',children:[
      {id: 1, name: '用户管理', icon:'el-icon-user',path:'/admin',fullPath:'/set/admin'},
      {id: 2, name: '角色管理', icon:'el-icon-user',path:'/role',fullPath:'/set/role'},
      {id: 3, name: '菜单管理', icon:'el-icon-menu',path:'/menu',fullPath:'/set/menu'},
    ]},
    {id: 10, name: '组件', icon: 'el-icon-platform-eleme', path: '/assembly', children: [
      {id: 1, name: '表单', icon: 'el-icon-menu', path: '/form', fullPath: '/assembly/form'},
      {id: 2, name: '表格', icon: 'el-icon-menu', path: '/table', fullPath: '/assembly/table'},
      {id: 3, name: '测试', icon: 'el-icon-menu', path: '/test', fullPath: '/assembly/test'},
    ]},
  ],
  activePath: '/home/survey',
  tabId: 0,
  isCollapseMenu: false,
  navBreadcrumbs: ['首页', '概况'],
  navTabs: [
    {name: '概况', path: '/home/survey'}
  ]
}

const getters = {

}

const mutations = {

  //更新激活菜单
  setActivePath(state: any, payload: string) {
    state.activePath = payload
  },

  //更新tabId
  setTabId(state: any, payload: string) {
    state.tabId = state.menuData.findIndex((item: any) => item.path.includes(payload))
  },

  //更新面包屑
  setNavBreadcrumbs(state: any, payload: any) {
    if (payload[0] === 'login') return
    let index = 0, result: any = []
    const recursion = (data: any, arr: any, i: number) => {
      let target = data.find((item: any) => item.path.includes(arr[i]))
      result.push(target.name)
      index++

      if (target.children) {
        recursion(target.children, arr, index)
      }
    }
    
    recursion(state.menuData, payload, index)
    state.navBreadcrumbs = result
  },

  //更新标签栏
  setNavTabs(state: any, payload: any) {
    if (typeof payload === 'object') {
      if (state.navTabs.find((item: any) => item.name === payload.name || payload.name === '登录')) return
      state.navTabs.push(payload)
    } else {
      let targetIndex = state.navTabs.findIndex((item: any) => item.path === payload)
      state.navTabs.splice(targetIndex, 1)
    }
  },

  //折叠展开菜单
  toggle(state: any) {
    state.isCollapseMenu = !state.isCollapseMenu
  },
}

const actions = {
  
}

export default createStore({
  state,
  getters,
  mutations,
  actions
})


